package com.example.beikeyigou.setting;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.login_reg.UserBean;
import com.example.beikeyigou.utils.BaseActivity;
import com.example.beikeyigou.utils.SaveUserUtils;
import com.example.beikeyigou.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class SecurityQuesActivity extends BaseActivity {
    private UserBean.DataBean userData, newUserData;
    private EditText answerEt;
    private TextView questionTv;
    private Button confirmBtn;
    private ImageView backImg;

    int flag = 0;       // 0：向服务器查询问题 / 1：向服务器验证密保问题

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_security_question);

        findView();
        refreshData();
        setListener();
        changeStatusBarTextImgColor(true);
    }

    public void changeStatusBarTextImgColor(boolean color) {
        if (color) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        } else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
        }
    }

    private void setListener() {
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String answer = answerEt.getText().toString();
                if (answer.equals("")) {
                    Toast.makeText(SecurityQuesActivity.this, "输入不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("id", String.valueOf(userData.getId()));
                map.put("answer", answer);
                postNetwork(URLUtils.verifyAnswer_url, map);
            }
        });

        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void refreshData() {
        userData = SaveUserUtils.getUserData(this);
        newUserData = new UserBean.DataBean(userData.getId(), userData.getPhone(),
                userData.getPassword(), userData.getName(), userData.getAddress(),
                userData.getNickname(), userData.getQuestion(), userData.getAnswer());
        if (userData == null) {
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            finish();
        }
        // 向服务器查询密保问题
        getNetwork(URLUtils.showQuestion_url+"?id="+String.valueOf(userData.getId()));
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        if (flag == 0) {
            switch (code) {
                case 200:       // 修改成功
                    Toast.makeText(this, "查询成功！", Toast.LENGTH_SHORT).show();
                    questionTv.setText(bean.getMessage());
                    break;
                case 201:
                    Toast.makeText(this, "未设置安全问题！", Toast.LENGTH_SHORT).show();
                    break;
                default:        // 其他，修改失败
                    Toast.makeText(this, "查询失败", Toast.LENGTH_SHORT).show();
                    break;
            }
            flag = 1;
        } else if (flag == 1) {
            switch (code) {
                case 200:       // 验证通过
                    Toast.makeText(this, "验证通过！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(SecurityQuesActivity.this, NewPwd2Activity.class);
                    startActivity(intent);
                    break;
                case 201:       // 答案错误
                    Toast.makeText(this, "答案错误！", Toast.LENGTH_SHORT).show();
                    break;
                default:        // 其他，验证失败
                    Toast.makeText(this, "验证失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    private void findView() {
        answerEt = findViewById(R.id.securityQues_answer);
        confirmBtn = findViewById(R.id.securityQues_btn);
        questionTv = findViewById(R.id.securityQues_question);
        backImg = findViewById(R.id.securityQues_back);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}